﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MyRoguelike.Tools;

namespace MyRoguelike.Game.Objects
{
    class GOCamera : GameObject
    {
        private Vector2 _offset;

        public GOCamera(Level level)
            : base(level)
        {
            // Get game viewport
            Viewport view = ResourceManager.Game.GraphicsDevice.Viewport;

            // Set offset
            _offset = new Vector2(view.Width/2, view.Height/2);
        }

        /// <summary>
        /// Returns offset for correct draw game objects
        /// </summary>
        /// <returns></returns>
        public Vector2 GetDrawOffset()
        {
            return Position - _offset;
        }

        public void LookAt(GameObject gameObject)
        {
            Position = gameObject.Position;
        }

        public override void Update(GameTime gameTime)
        {
            float speed = 4f;

            Vector2 offset = new Vector2();
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                offset.X = 1;
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
                offset.Y = 1;
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
                offset.X = -1;
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
                offset.Y = -1;

            Position = Position - (offset * (gameTime.ElapsedGameTime.Milliseconds / 16 * speed));
        }

        public override void Draw(SpriteBatch spriteBatch)
        {

        }

        public override void Step()
        {
            // Set camera position to player position
            LookAt(_level.Player);
        }
    }
}
